package com.czmh.etc.onlinepubsys.jy.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author chen_yang
 */
public interface IHttpHelper {

    /**
     * http post json
     *
     * @param requestDTO http post推送数据
     * @return 应答数据
     */
    ResponseDTO post(RequestDTO requestDTO);

    class RequestDTO {

        public String url;
        public String data;

        public RequestDTO setUrl(String url) {
            this.url = url;
            return this;
        }

        public RequestDTO setData(String data) {
            this.data = data;
            return this;
        }

        @Override
        public String toString() {
            return "RequestDTO{" +
                    "url='" + url + '\'' +
                    ", data='" + data + '\'' +
                    '}';
        }
    }

    class ResponseDTO {

        /**
         * 针对以下错误http状态码码特殊处理
         */
        private List<Integer> resendProcessForErrorHttpStateCode =
                new ArrayList<>(Arrays.asList(403, 404, 406, 407, 408, 411, 413, 421, 502, 503, 504));
        //客户端错误
        public static final int CLIENT_ERROR_CODE = 420;
        public static final int SERVER_NO_RESP = 520;
        //客户端错误
        public static final String CLIENT_ERROR = "client_error";
        //反序列失败
        public static final String RESP_DESERIALIZE_FAIL = "resp_deserialize_fail";
        //序列化失败
        public static final String REQ_SERIALIZE_FAIL = "req_serialize_to_string_fail";
        public static final String RESP_SERIALIZE_FAIL = "resp_serialize_to_string_fail";
        //空的请求数据
        public static final String EMPTY_REQ_DATA = "empty_req_data";
        //发送请求失败
        public static final String SEND_REQ_FAIL = "send_req_fail";

        private int code;
        private String msg;
        private String errorMsg;
        private String data;

        public boolean isSuccessful() {
            return code >= 200 && code < 300;
        }

        public ResponseDTO setMsg(String msg) {
            this.msg = msg;
            return this;
        }

        public ResponseDTO setCode(int code) {
            this.code = code;
            return this;
        }

        public ResponseDTO setData(String data) {
            this.data = data;
            return this;
        }

        public int getCode() {
            return code;
        }

        public String getMsg() {
            return msg;
        }

        public String getData() {
            return data;
        }

        public String getErrorMsg() {
            return errorMsg;
        }

        public ResponseDTO setErrorMsg(String errorMsg) {
            this.errorMsg = errorMsg;
            return this;
        }

        @Override
        public String toString() {
            return "ResponseDTO{" +
                    "code=" + code +
                    ", msg='" + msg + '\'' +
                    ", errorMsg='" + errorMsg + '\'' +
                    ", data='" + data + '\'' +
                    '}';
        }

        public boolean isResponseSuccess() {
            return isSuccessful();
        }
    }
}
